package com.example.mqdemo.PointToPoint.annotationType.config;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
 *@description: 点对点队列消息工厂配置
 *@author: hisen
 *@Time: 2023/11/20  23:30
 */
@Configuration
public class PointToPointConfiguration {

    @Bean(value = "pointToPointFactory")
    public SimpleRabbitListenerContainerFactory pointToPointFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        
        // 线程池配置
        factory.setConcurrentConsumers(3); // 设置并发消费者数量
        factory.setMaxConcurrentConsumers(10); // 设置最大并发消费者数量

        // 错误处理策略
        factory.setErrorHandler(data -> System.out.println("Error " + data));

        // 事务管理（如果需要）
        // factory.setTransactionManager(transactionManager);

        // 消息确认模式
        factory.setAcknowledgeMode(AcknowledgeMode.AUTO); // 或者 AcknowledgeMode.MANUAL

        // 设置连接工厂
        factory.setConnectionFactory(connectionFactory);

        return factory;
    }
}
